"use client";
import { FC, PropsWithChildren, useState } from "react";
import { useRouter } from "@/i18n";
import HeaderBack from "@/components/HeaderBack";
import GoogleCom from "./component/GoogleCom";
import FromCom from "./component/FromCom";
import DomainFooter from "@/components/DomainFooter";
import './page.scss'
import {getLoginApi} from "@/api/user";
import { useGlobalStore } from '@/stores';

interface Props {}

const Login: FC<PropsWithChildren<Props>> = () => {
    const { setToken, setUserInfo } = useGlobalStore();

    const router:any = useRouter()
    const [msgError, setMsgError] = useState('')
    const loginRequest = async ({userPhone, pwd}: any) => {
        let params = {user_phone: userPhone, pwd}
        let res = await getLoginApi(params)
        if(res.code == 200) {
            alert('登录成功')
            setUserInfo({...res.data, user_phone: userPhone})
            setToken(res.data.token)
            router.replace('/')
        }
        setMsgError(res.msg || '')
    }

    return (
        <div className="login-box">
            <HeaderBack />
            <GoogleCom />
            <FromCom callbackFun={loginRequest} msgError={msgError}/>
            <DomainFooter />
        </div>
    );
};

export default Login;
